package com.itheima.dubbo.mongodb.impl;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.commons.model.mongo.Comment;
import com.itheima.commons.model.vo.PageResult;
import com.itheima.dubbo.interfaces.CommentApi;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class CommentApiImpl implements CommentApi {

    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * 保存点赞/评论/喜欢数据到MongoDB
     *
     * @param comment
     * @return
     */
    @Override
    public Long saveComment(Comment comment) {
        mongoTemplate.save(comment);
        return this.queryCommentCount(comment.getPublishId().toHexString(), comment.getCommentType());
    }


    /**
     * 取消点赞/取消喜欢
     *
     * @return
     */
    @Override
    public Long delComment(Long userId, String publishId, Integer type) {
        mongoTemplate.remove(Query.query(Criteria.where("publishId").is(new ObjectId(publishId))
                .and("userId").is(userId).and("commentType").is(type)), Comment.class);
        return this.queryCommentCount(publishId, type);
    }


    /**
     * 查询是否点赞/是否喜欢
     *
     * @param userId
     * @param publishId
     * @return
     */
    @Override
    public Boolean queryUserIsLikeORIsLove(Long userId, String publishId, Integer type) {
        List<Comment> comments = mongoTemplate.find(Query.query(Criteria.where("publishId").is(new ObjectId(publishId))
                .and("userId").is(userId).and("commentType").is(type)
        ), Comment.class);
        return CollUtil.isNotEmpty(comments);
    }


    /**
     * 查询喜欢数/评论数/点赞数
     *
     * @param publishId
     * @param type
     * @return
     */
    @Override
    public Long queryCommentCount(String publishId, Integer type) {
        return mongoTemplate.count(Query.query(Criteria.where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(type)), Comment.class);
    }


    /**
     * 查询评论列表
     *
     * @param publishId
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult queryCommentList(String publishId, Integer page, Integer pageSize) {
        List<Comment> comments = mongoTemplate.find(Query.query(Criteria.where("publishId").is(new ObjectId(publishId))
                .and("commentType").is(2))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page - 1, pageSize)), Comment.class);
        return new PageResult(page, pageSize, 0L, comments);
    }


    /**
     * 根据评论id查询评论对象
     *
     * @param publishId
     * @return
     */
    @Override
    public Comment queryCommentById(String publishId) {
        return mongoTemplate.findOne(Query.query(
                Criteria.where("id").is(publishId)), Comment.class);
    }



    /**
     * 查询对某个人的点赞/评论/喜欢列表
     *
     * @param page
     * @param pageSize
     * @param userId
     * @param commentType
     * @return
     */
    @Override
    public List<Comment> queryCommentListByUserId(Integer page, Integer pageSize, Long userId, Integer commentType) {
        return mongoTemplate.find(Query.query(Criteria.where("publishUserId").is(userId)
                .and("commentType").is(commentType))
                .with(Sort.by(Sort.Order.desc("created")))
                .with(PageRequest.of(page - 1, pageSize)), Comment.class);
    }

    @Override
    public List<Comment> queryCommentByuid(Long publishId) {
        return mongoTemplate.find(Query.query(Criteria.where("publishUserId").is(publishId)), Comment.class);
    }
}
